<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="models.Course"%>
<%@page import="models.User"%>
<%@page import="service.UserDao"%>
<%@page import="org.hibernate.Session"%>
<%@page import="service.HibernateUtil"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link href="style/tutor.css" type="text/css" rel="Stylesheet">
        <!-- include jQuery Library From Google Code -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function(){
                //smooth pagina overloop
                $("body").css("display", "none"); 
                $("body").fadeIn("slow");
                
                $("#floatingMenu").click(function () {
                    $('#floatingMenuContent').slideToggle();
                });
            });
            /*
            $(document).ready(function(){                
                function queryFile(){
                    var file = $("#fileselect").val();
                    if(file != null){
                        window.location = "/files?courseID=${courseID}&selectedfile="+file;
                        //document.forms["filesform"].submit();
                    }
                }
                $("#fileselect").bind("change", queryFile);

            });*/
            function newFolder(){
                var folderName = prompt("Enter folder name : ", "folder name here");
                if (folderName != null){
                    window.location = "/files?courseID=${courseID}&foldername="+folderName;
                }
            }
            var descriptions = new Array();
            <c:forEach var="description" items="${descriptions}">
                var description = new Object();
                description.fileName = "${description.fileName}";
                description.fileDescription = "${description.fileDescription}";
                descriptions.push(description);
            </c:forEach>
                function fileInfo(filename){
                    for(var i=0; i<descriptions.length; i++) {
                        
                        var value = descriptions[i].fileName;
                        if(value == filename){
                            alert(descriptions[i].fileDescription);
                            break;
                        }
                    }
                    
                }
            
                function get_filename(obj) {
                    var filename = document.getElementById("hiddenfilename");
                    var file = obj.value;
                    filename.value = file;
                }
        </script>
        <jsp:useBean id="inhoud" scope="request" class="models.Bean" />   
        <jsp:setProperty name="inhoud" property="language" value="<%= models.Bean.getLanguage()%>" />
        <title>
            Three 2 Learn | <jsp:setProperty name="inhoud" property="key" value="Files" />
            <jsp:getProperty name="inhoud" property="key" />
        </title>
    </head>
    <body>       
        <div id="wrapper">
            <jsp:include page="sidebar.jsp"/>
            <div id="right">
                <jsp:include page="courseDescriptionList.jsp"/>

                <h1><jsp:setProperty name="inhoud" property="key" value="FileManager" />
                    <jsp:getProperty name="inhoud" property="key" /></h1>
                    <c:choose>
                        <c:when test="${not empty courseID}">
                        <div>
                            <h2><jsp:setProperty name="inhoud" property="key" value="UploadFile" />
                                <jsp:getProperty name="inhoud" property="key" /></h2>

                            <form action="/files" method="post"
                                  enctype="multipart/form-data">
                                <table>
                                    <th colspan="2"><jsp:setProperty name="inhoud" property="key" value="ChooseAFile" />
                                        <jsp:getProperty name="inhoud" property="key" /></th>
                                    <tr>
                                        <td>
                                            Description:
                                            <input type="text" id="description" name="description" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="file" name="file" size="50" />
                                        </td>
                                    </tr>

                                    <tr>
                                        <td>
                                            <input type="submit" value="Upload File" />
                                        </td>
                                    </tr>
                                </table>
                            </form>

                        </div>
                                    <div>
                                        Modulebeschrijving:
                                        <form method="post">
                                        <textarea name="folderdescription">${folderdescription}</textarea>
                                        <input type="submit" value="Opslaan">
                                        </form>
                                    </div>
                        <div id="files">
                            <h2>Browser</h2>
                            <!--<h2></h2>
                            <p><a href="#" onclick="newFolder();">create new folder</a></p>!-->
                            <table width="100%" id="filetable">
                                <th colspan="3"><jsp:setProperty name="inhoud" property="key" value="Location" />
                                    <jsp:getProperty name="inhoud" property="key" />: ${foldername}${subfolder}
                                    <a class="button float" href="#" onclick="newFolder();"><jsp:setProperty name="inhoud" property="key" value="CreateNewFolder" />
                                        <jsp:getProperty name="inhoud" property="key" /> </a>
                                </th>
                                <c:if test="${subfolder != ''}">                                    
                                    <tr>
                                        <td> 
                                            <img height="40px" src="/images/foldericon.png" />
                                        </td>
                                        <td  style="width:90%;">
                                            <p align="center"><a href="/files?courseID=${courseID}&selectedfile=..">..</a></p>
                                        </td><td></td>
                                    </tr>
                                </c:if>
                                <c:forEach var="directory" items="${directories}">
                                    <tr>
                                        <td>
                                            <img height="40px" src="/images/foldericon.png" />
                                        </td>
                                        <td style="width:90%;">
                                            <p align="center"><a href="/files?courseID=${courseID}&selectedfile=${directory.name}">${directory.name}</a></p>
                                        </td><td></td>
                                    </tr>
                                </c:forEach>
                                <c:forEach var="file" items="${files}">
                                    <tr>
                                        <c:set var="fileExtension" value="fileicon.png"/>
                                        <c:if test="${fn:contains(file.name, '.ppt')}">
                                            <c:set var="fileExtension" value="ppticon.png"/>
                                        </c:if>
                                        <c:if test="${fn:contains(file.name, '.doc')}">
                                            <c:set var="fileExtension" value="docicon.jpg"/>
                                        </c:if>
                                        <c:if test="${fn:contains(file.name, '.pdf')}">
                                            <c:set var="fileExtension" value="pdficon.jpg"/>
                                        </c:if>
                                        <c:if test="${fn:contains(file.name, '.txt')}">
                                            <c:set var="fileExtension" value="txticon.gif"/>
                                        </c:if>
                                        <c:if test="${fn:contains(file.name, '.zip')}">
                                            <c:set var="fileExtension" value="zipicon.jpg"/>
                                        </c:if>

                                        <td><img height="40px" src="/images/${fileExtension}" />
                                        </td>
                                        <td width="800px">
                                            <p align="center">${file.name}</p>
                                        </td>
                                        <td>
                                            <a href="#" onclick="fileInfo('${file.name}');"><img height="20" src="/images/info.png"/></a>
                                        </td>
                                        <td>
                                            <a href="/files?courseID=${courseID}&selectedfile=${file.name}">Download</a>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </table>
                            <input type="hidden" name="subfolder" id="subfolder" value="${subfolder}"></input>
                        </div>
                    </c:when>
                    <c:otherwise>
                        <div id="courseList">
                            <h2><jsp:setProperty name="inhoud" property="key" value="SelectACourse" />
                                <jsp:getProperty name="inhoud" property="key" /></h2>
                            <table width="100%">
                                <tr>
                                    <c:forEach var="course" items="${courses}" varStatus="loop">
                                        <c:if test="${not loop.first and loop.index % 3 == 0}">
                                        </tr></tr>
                                    </c:if>
                                    <td width="20%"> 
                                        <a href="/files?courseID=${course.courseID}">${course.name}</a>
                                    </td>
                                </c:forEach>
                                </tr>
                            </table>
                        </div>
                    </c:otherwise>
                </c:choose>

            </div>
        </div>
    </body>
</html>
